#include "head.h"

// 单链表的创建
D_list *R_List_init()
{
    // 创建头节点
    D_list *list = (D_list *)malloc(sizeof(D_list));
    // 判断是否创建成功
    if (list != NULL)
    {
        // 如果成功则初始化链表中的头节点为 NULL
        list->next = NULL;
    }
    else
    {
        printf("创建失败\n");
        return NULL;
    }
    // 返回链表结构体
    return list;
}

// 单链表的插入
void R_List_insert(D_list *list, D_data data, int pos)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }

    D_node *new_node = (D_node *)malloc(sizeof(D_node));
    if (new_node == NULL)
    {
        printf("new_node创建失败\n");
        return;
    }
    new_node->next = NULL;
    new_node->data = data;

    if (pos == 1)
    {
        new_node->next = list->next;
        list->next = new_node;
        return;
    }

    D_node *ptr = list->next;

    for (size_t i = 1; i < pos - 1 && ptr != NULL; i++)
    {
        ptr = ptr->next;
    }

    if (ptr == NULL)
    {
        printf("ptr已经指向NULL\n");
        free(new_node);
        return;
    }
    new_node->next = ptr->next;
    ptr->next = new_node;

    return;
}

// 单链表的删除
void R_List_delete(D_list *list, int pos)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }
    if (list->next == NULL)
    {
        printf("链表无数据\n");
        return;
    }

    D_node *ptr_1 = list->next;
    D_node *ptr_2 = NULL;
    if (pos == 1)
    {
        list->next = ptr_1->next;
        free(ptr_1);
        return;
    }

    for (size_t i = 0; i < pos - 1 && ptr_1->next != NULL; i++)
    {
        ptr_2 = ptr_1;
        ptr_1 = ptr_1->next;
    }
    if (ptr_1 == NULL)
    {
        printf("ptr_1指向null\n");
        return;
    }
    ptr_2->next = ptr_1->next;

    free(ptr_1);
    return;
}

// 单链表的查找
D_node *R_List_find_name(D_list *list, char *name)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return NULL;
    }

    D_node *ptr = list->next;
    while (ptr != NULL)
    {
        if (strcmp(ptr->data.name, name) == 0)
        {
            return ptr;
        }
        ptr = ptr->next;
    }

    return NULL;
}

D_node *R_List_find_id(D_list *list, int id)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return NULL;
    }

    D_node *ptr = list->next;
    while (ptr != NULL)
    {
        if (ptr->data.ticket.id == id)
        {
            return ptr;
        }
        ptr = ptr->next;
    }

    return NULL;
}

int R_List_find_name_n(D_list *list, char *name)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return 0;
    }
    int i = 1;
    D_node *ptr = list->next;
    while (ptr != NULL)
    {
        if (strcmp(ptr->data.name, name) == 0)
        {
            return i;
        }
        ptr = ptr->next;
        i++;
    }
    return 0;
}

int R_List_find_id_n(D_list *list, int id,char *name)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return 0;
    }
    int i = 1;
    D_node *ptr = list->next;
    while (ptr != NULL)
    {
        if (ptr->data.ticket.id == id && strcmp(ptr->data.name, name) == 0)
        {
            return i;
        }
        ptr = ptr->next;
        i++;
    }
    return 0;
}
// 单链表的遍历
void R_List_print(D_list *list)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }
    if (list->next == NULL)
    {
        printf("链表无数据\n");
        return;
    }
    D_node *ptr = list->next;
    while (ptr != NULL)
    {
        printf("用户名:%s\t",ptr->data.name);
        printf("航班班次: %d, 机型: %s, 起点站: %s, 终点站: %s, 起飞时间: %02d:%02d, 到达时间: %02d:%02d, 飞行时间: %s, 票价: %d, 额定载客量: %d, 剩余座位: %d, 是否起飞: %c\n",  
                ptr->data.ticket.id, ptr->data.ticket.flighttype, ptr->data.ticket.startcity, ptr->data.ticket.arrivecity,  
                ptr->data.ticket.starttime[0], ptr->data.ticket.starttime[1], ptr->data.ticket.arrivetime[0], ptr->data.ticket.arrivetime[1],  
                ptr->data.ticket.flytime, ptr->data.ticket.value, ptr->data.ticket.maxNUM, ptr->data.ticket.leftbuyersa, ptr->data.ticket.whetherfly); 
        ptr = ptr->next;
    }
}

// 单链表的清空
void R_List_clear(D_list *list)
{
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }
    if (list->next == NULL)
    {
        printf("链表无数据\n");
        return;
    }

    D_node *ptr_1 = list->next;
    D_node *ptr_2 = ptr_1->next;

    while (ptr_1 != NULL)
    {
        free(ptr_1);
        ptr_1 = ptr_2;
        if (ptr_2 != NULL)
        {
            ptr_2 = ptr_2->next;
        }
    }
    list->next = NULL;
}

// 单链表的销毁
void R_List_free(D_list **list)
{
    if (list == NULL || *list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }

    D_node *ptr_1 = (*list)->next;
    D_node *ptr_2 = ptr_1->next;

    while (ptr_1 != NULL)
    {
        free(ptr_1);
        ptr_1 = ptr_2;
        if (ptr_2 != NULL)
        {
            ptr_2 = ptr_2->next;
        }
    }

    free(*list);
    *list = NULL;
}

// 向文件写入用户信息
void R_List_File_print(D_list *list, const char *file_name)
{
    // 安全判断
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }
    if (list->next == NULL)
    {
        printf("链表无数据\n");
        return;
    }

    FILE *fp = fopen(file_name, "w+");
    if (fp == NULL)
    {
        printf("文件创建失败\n");
        return;
    }
    fseek(fp, 0, SEEK_SET);

    D_node *ptr = list->next;

    while (ptr != NULL)
    {
        
        fprintf(fp ,"用户名:%s\t航班班次:%d\t机型:%s\t起点站:%s\t终点站:%s\t起飞时间:%02d:%02d\t到达时间:%02d:%02d\t飞行时间:%s\t票价:%d\t额定载客量:%d\t剩余座位:%d\t是否起飞:%c\n",  
        ptr->data.name,ptr->data.ticket.id, ptr->data.ticket.flighttype, ptr->data.ticket.startcity, ptr->data.ticket.arrivecity,  
        ptr->data.ticket.starttime[0], ptr->data.ticket.starttime[1], ptr->data.ticket.arrivetime[0], ptr->data.ticket.arrivetime[1],  
        ptr->data.ticket.flytime, ptr->data.ticket.value, ptr->data.ticket.maxNUM, ptr->data.ticket.leftbuyersa, ptr->data.ticket.whetherfly);
        ptr = ptr->next;
    }

    fflush(fp); // 刷新文件

    fclose(fp);
}

// 从文件读取订单信息  
void R_List_File_scanf(D_list *list, const char *file_name) 
{
    // 安全判断
    if (list == NULL)
    {
        printf("链表为NULL\n");
        return;
    }

    FILE *fp = fopen(file_name, "r");
    if (fp == NULL)
    {
        printf("文件打开失败\n");
        return;
    }
    // 移动光标
    fseek(fp, 0, SEEK_SET);
    // 清空链表
    R_List_clear(list);

    D_data data;
    char ch = 0;
    int pos = 0;

    // 检测文件行数
    while ((ch = fgetc(fp)) != EOF)
    {
        if (ch == '\n')
        {
            pos++;
        }
    }

    // 移动光标
    fseek(fp, 0, SEEK_SET);

    for (size_t i = 0; i < pos; i++)
    {
        //用户名:关羽	航班班次:5	机型:Boeing_777	起点站:上海	终点站:北京	起飞时间:15:00	到达时间:17:10	飞行时间:2h10m	票价:272	额定载客量:240	剩余座位:234	是否起飞:n
        fscanf(fp ,"用户名:%s\t航班班次:%d\t机型:%s\t起点站:%s\t终点站:%s\t起飞时间:%02d:%02d\t到达时间:%02d:%02d\t飞行时间:%s\t票价:%d\t额定载客量:%d\t剩余座位:%d\t是否起飞:%c\n",  
         data.name,&data.ticket.id, data.ticket.flighttype, data.ticket.startcity, data.ticket.arrivecity, &data.ticket.starttime[0], &data.ticket.starttime[1],
         &data.ticket.arrivetime[0], &data.ticket.arrivetime[1], data.ticket.flytime, &data.ticket.value, &data.ticket.maxNUM, &data.ticket.leftbuyersa, &data.ticket.whetherfly);
        R_List_insert(list, data, i + 1);
    }
    fflush(fp); // 刷新文件
    fclose(fp);
}

// 查找并打印符合条件的订单 
int findOrder(D_list *head, const char *name) 
{  
    D_node *current = head->next; 
    int found = 0;  
    if (current == NULL) 
    {  
        printf("订单列表为空。\n");  
        return 0;  
    }  
    printf("您的订单有：\n"); 
    while (current != NULL)
    {  
        if (strcmp(current->data.name, name) == 0) 
            {  
            // 打印订单航班信息  
            printf("班号：%d 机型：%s,从%s到%s,起飞时间:%02d:%02d,到达时间:%02d:%02d,票价:%d,剩余座位数:%d\n", current->data.ticket.id, current->data.ticket.flighttype, current->data.ticket.startcity, current->data.ticket.arrivecity,
                    current->data.ticket.starttime[0],current->data.ticket.starttime[1],current->data.ticket.arrivetime[0],current->data.ticket.arrivetime[1],current->data.ticket.value,current->data.ticket.leftbuyersa);  
            found = 1;  
            }  
        current = current->next; 
    }  
    return found;
}  
// 查找是否买过相同的机票
int Find_Same_Order(D_list *head, const char *name ,int id) 
{  
    D_node *current = head->next; 
    int found = 0;  
    if (current == NULL) 
    {  
        printf("订单列表为空。\n");  
        return 0;  
    }  
    while (current != NULL)
    {  
        if (strcmp(current->data.name, name) == 0 && current->data.ticket.id == id) 
            {   
                found = 1;
                return found;   
            }  
        current = current->next; 
    }  
    return found;
}  